﻿using System.Linq;
using System.Web.Mvc;
using Realtors.Repository;
using Realtors.Web.UI.Infrastructure;
using Realtors.Web.UI.Models;
using System;

namespace Realtors.Web.UI.Controllers
{
    public class HomeController : Controller
    {

        private IHouseRepository houseRepository { get; set; }

        public HomeController(IMailService contactSender, IHouseRepository HouseRepository)
        {
            houseRepository = HouseRepository;
        }

        public ActionResult Index()
        {
            var activeFeaturedListings= (from h in houseRepository.GetActiveFeaturedHouses()
                                                 select new HouseSummaryModel {City = h.HouseAddress.City,
                                                 Country = h.HouseAddress.Country, Description = h.Description,
                                                 HouseID =h.EntityId,IsSale=h.ForSale==true,NoOfBathrooms=h.NoOfBathrooms,
                                                 NoOfRooms=h.NoOfRooms,Price = h.ForSale==true?h.SellingPrice:h.LeasingPrice,
                                                 TotalVisits = h.TotalVisits,
                                                 SquareFeet = h.SquareFeet,State = h.HouseAddress.State,StreetAddress= h.HouseAddress.StreetAddress,
                                                 Title = h.Title,PropertyPhotos=h.PropertyPhotos, AdDate=h.DateAdded.ToShortDateString()});
                              

            HomeIndexViewModel model = new HomeIndexViewModel
            {
                FeaturedHousesForSale = activeFeaturedListings.Where(c=>c.IsSale==true),
                FeaturedHousesForLease = activeFeaturedListings.Where(c=>c.IsSale==false)
            };

            return View(model);
        }

        public ActionResult Rentals()
        {
            var activeListingsForRent = houseRepository.GetActiveHouses().Where(c => c.ForLease == true);

            RentalsViewModel model = new RentalsViewModel
            {
                RentalUnits = activeListingsForRent.ToList()
            };

            //we may use the home index view, however maybe there will some special features reqgarding to rental units
            return View(model);
        }

        public ActionResult Sale()
        {
            var activeListingsForSale = houseRepository.GetActiveHouses().Where(c => c.ForSale == true);

            SalesViewModel model = new SalesViewModel
            {
                House4Sale = activeListingsForSale.ToList()
            };

            //we may use the home index view, however maybe there will some special features reqgarding to sale units
            return View(model);
        }

        public ActionResult Detail(Guid houseID)
        {
            var house = houseRepository.GetHouseByID(houseID);
            house.TotalVisits++;
            houseRepository.UpdateHouse(house);
            houseRepository.Save();
            HouseSummaryModel model = new HouseSummaryModel();
            model.AdDate = house.DateAdded.ToShortDateString();
            model.City = house.HouseAddress.City;
            model.Country = house.HouseAddress.Country;
            model.Description = house.Description;
            model.HouseID = house.EntityId;
            model.IsSale = house.ForSale == true;
            model.NoOfBathrooms = house.NoOfBathrooms;
            model.NoOfRooms = house.NoOfRooms;
            model.Price = house.ForSale == true ? house.SellingPrice : house.LeasingPrice;
            model.PropertyPhotos = house.PropertyPhotos;
            model.SquareFeet = house.SquareFeet;
            model.State = house.HouseAddress.State;
            model.StreetAddress = house.HouseAddress.StreetAddress;
            model.Title = house.Title;
            model.TotalVisits = house.TotalVisits;           

            return View(house);
        }
    }
}